home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / inventor / www / workarounds / TextureDump.2.0.C < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  4.2 KB  |  130 lines

  1. //
  2. // The Inventor 2.0 texture code can cause other classes to be
  3. // incorrectly deleted, leading to core dumps (a core dump in the
  4. // cache element class is an example).  This code, taken directly from
  5. // the Inventor source for all Inventor releases after 2.0, corrects
  6. // the problem.
  7. //
  8. // To apply this patch, compile this file into a .o and then link
  9. // the .o before -lInventor.  The linker will give a warning about
  10. // multiply defined symbols; that is normal and expected.
  11. //
  12.  
  13. #include <GL/glu.h>
  14. #include <GL/gl.h>
  15. #include <Inventor/elements/SoCacheElement.h>
  16. #include <Inventor/elements/SoGLTextureEnabledElement.h>
  17. #include <Inventor/elements/SoGLTextureImageElement.h>
  18. #include <Inventor/elements/SoGLTextureQualityElement.h>
  19. #include <Inventor/errors/SoDebugError.h>
  20. #include <Inventor/misc/SoState.h>
  21.  
  22. ////////////////////////////////////////////////////////////////////////
  23. //
  24. // Description:
  25. //    Does the right GL stuff.  This takes a GL display list that can
  26. //    be used to render the texture; if -1 is passed in as the display
  27. //    list, this will try to build a display list (if there are none
  28. //    already open) and returns the display list, which must be freed
  29. //    by the node that sets this element.
  30. //
  31. // Use: public, static
  32.  
  33. int
  34. SoGLTextureImageElement::set(SoState *state, SoNode *node,
  35.                  const SbVec2s &size, int nc,
  36.                  const unsigned char *b,
  37.                  int dl)
  38. //
  39. ////////////////////////////////////////////////////////////////////////
  40. {
  41.     SoGLTextureImageElement    *elt;
  42.  
  43.     if (size[0] == 0 || size[1] == 0 || nc == 0) {
  44.     // Disable texturing
  45.     SoGLTextureEnabledElement::set(state, FALSE);
  46.     return -1;
  47.     }
  48.  
  49.     // Enable texturing IF textureQuality is > 0:
  50.     // Note: the texture must still be sent to GL, because the
  51.     // textureQuality may change (if it does, the texture quality
  52.     // element enables texturing).
  53.     if (SoGLTextureQualityElement::get(state) > 0) {
  54.     SoGLTextureEnabledElement::set(state, TRUE);
  55.     }
  56.  
  57.     // Get an instance we can change (pushing if necessary)
  58.     elt = (SoGLTextureImageElement *) getElement(state, classStackIndex, node);
  59.  
  60.     if (elt != NULL) {
  61.     elt->SoTextureImageElement::setElt(size, nc, b);
  62.  
  63.     elt->displayList = dl;
  64.     elt->send(state);
  65.     return elt->displayList;
  66.     }
  67.     return -1;
  68. }
  69.  
  70. ////////////////////////////////////////////////////////////////////////
  71. //
  72. // Description:
  73. //    Sends down a 2D texture.  Builds or uses a display list, if it
  74. //    can.
  75. //
  76. // Use: private
  77.  
  78. void
  79. SoGLTextureImageElement::send(SoState *state)
  80. //
  81. ////////////////////////////////////////////////////////////////////////
  82. {
  83.     if (displayList != -1) {
  84.     // use display list
  85.     glCallList(displayList);
  86.     return;
  87.     }
  88.  
  89.     SbBool buildList = !SoCacheElement::anyOpen(state);
  90.     if (buildList) {
  91.     displayList = glGenLists(1);
  92.     glNewList(displayList, GL_COMPILE_AND_EXECUTE);
  93.     }
  94.  
  95.     // Optimization; don't change the default UNPACK_ALIGNMENT if the
  96.     // texture's width happens to work out to a multiple of 4:
  97.     if ((size[0]*numComponents)%4 != 0)
  98.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);  // Not default
  99.  
  100.     // Set all pixel transfer modes.  Not only does this guarantee that
  101.     // they are set correctly, but OpenGL implementations may be
  102.     // better able to optimize display lists if all of these commands
  103.     // are in the list.
  104.     glPixelTransferf(GL_RED_SCALE, 1.0);  // Default
  105.     glPixelTransferf(GL_GREEN_SCALE, 1.0);  // Default
  106.     glPixelTransferf(GL_BLUE_SCALE, 1.0);  // Default
  107.     glPixelTransferf(GL_ALPHA_SCALE, 1.0);  // Default
  108.     glPixelTransferf(GL_RED_BIAS, 0.0);  // Default
  109.     glPixelTransferf(GL_GREEN_BIAS, 0.0);  // Default
  110.     glPixelTransferf(GL_BLUE_BIAS, 0.0);  // Default
  111.     glPixelTransferf(GL_ALPHA_BIAS, 0.0);  // Default
  112.     
  113.     
  114.     int format;
  115.     if (numComponents == 1) format = GL_LUMINANCE;
  116.     else if (numComponents == 2) format = GL_LUMINANCE_ALPHA;
  117.     else if (numComponents == 3) format = GL_RGB;
  118.     else if (numComponents == 4) format = GL_RGBA;
  119.  
  120.     gluBuild2DMipmaps(GL_TEXTURE_2D, numComponents, 
  121.               size[0], size[1], format, GL_UNSIGNED_BYTE,
  122.               (const void *)bytes);
  123.     if ((size[0]*numComponents)%4 != 0)
  124.     glPixelStorei(GL_UNPACK_ALIGNMENT, 4);  // Reset to default
  125.  
  126.     if (buildList) {
  127.     glEndList();
  128.     }
  129. }
  130.